<?php

function skip($url,$pic,$message){
$html=<<< A
    <!DOCTYPE html>
    <html>
        <head>
            <title>页面跳转中</title>
            <meta charset="utf-8" />
            <meta http-equiv="refresh" content="5;url={$url}">
            <link rel="stylesheet" href="/Project_bbs/css/admin.css"/>
        </head>
        <body class="remind">
            <div class="notice"><span class="pic {$pic}"></span> {$message} 5秒后页面自动跳转<a style="color:#266aae;" href="{$url}">（点击此处立即跳转）</a> </div>
        </body>
    </html>
A;
    echo $html;
    exit();   

    }

//数据去html特性
function dehtml($data){
	if(is_string($data)){
		return htmlspecialchars($data);
	}
	if(is_array($data)){
		foreach($data as $key=>$val){
			$data[$key]=dehtml($val);
		}
		return $data;
	}
}
//保留数据换行特性
function tonl2br($data){
	if(is_string($data)){
		return nl2br($data);
	}
	if(is_array($data)){
		foreach($data as $key=>$val){
			$data[$key]=tonl2br($val);
		}
		return $data;
	}
}


function vcode($width=120,$height=45,$count=4,$fontSize=28){
    header('Content-type:image/jpeg');
    $vcode='';
    $element=array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
    'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
    for($i=0;$i<$count;$i++){
        $vcode.=$element[rand(0,count($element)-1)];
    }
    $img=imagecreatetruecolor($width,$height);
    $colorBg=imagecolorallocate($img,rand(200,250),rand(200,250),rand(200,250));
    $colorDox=imagecolorallocate($img,rand(100,150),rand(100,150),rand(100,150));
    $colorText=imagecolorallocate($img,rand(0,75),rand(0,75),rand(0,75));
    imagefill($img,0,0,$colorBg);
    for($i=0;$i<150;$i++){
        imagesetpixel($img,rand(1,$width-1),rand(1,$height-1),$colorDox);
    }
    
    //imagestring($img,5,10,5,"abcd",$colorText);
    imagettftext($img,$fontSize,rand(-5,5),2,rand($height-6,$height-12),$colorText,'D:/CS/Apache002/htdocs/Project_bbs/font/diebel.ttf',$vcode);
    for($i=0;$i<3;$i++){
        $a1=rand(0,$width/4);$b1=rand(5,$height-5);$a2=rand($a1+$width/2,$width);$b2=rand(5,$height-5);
            imageline($img,$a1,$b1,$a2,$b2,$colorDox);
        }
    imagejpeg($img);
    imagedestroy($img);
    return $vcode;
    }
    
    function is_login($link){
        if(isset($_COOKIE['member']['name'])&&isset($_COOKIE['member']['pw'])){
            $query="select *from member where name='{$_COOKIE['member']['name']}' and sha1(pw)='{$_COOKIE['member']['pw']}'";
            $result=execute($link,$query);
            if(mysqli_affected_rows($link)==1){
                $data=mysqli_fetch_assoc($result);
                return $data['id'];
            }else{
                return false;
            }
        }else{
            return false;
        }
    }

    function pages($count_all,$count_cur=5,$num_btn=6){
        $html="";
        $pages=ceil($count_all/$count_cur);
        if(!isset($_GET['page'])||!is_numeric($_GET['page'])||$_GET['page']<1||empty($_GET['page'])){
            $_GET['page']=1;
        }
        if($_GET['page']>$pages){
            $_GET['page']=$pages;
        }
        $start=$_GET['page']-ceil($num_btn/2)+1;
        if(($start+$num_btn-1)>$pages){
            $start=$pages-$num_btn+1;
        }
        if($start<1){
            $start=1;
        }
        $arr=array();
        $detai=parse_url($_SERVER['REQUEST_URI']);
        if(isset($detai['query'])){
            parse_str($detai['query'],$arr);
            if(isset($arr['page'])){
                unset($arr['page']);
                $data=http_build_query($arr);
            }else{
                $data=($detai['query']);
            }
            //var_dump($data);exit;
        }else{
            $data='';
        }
        $pre=$_GET['page']-1;
        $next=$_GET['page']+1;
        if($_GET['page']!=1){
            $html.="<a href='$_SERVER[SCRIPT_NAME]?{$data}&page={$pre}'>« 上一页</a>  ";
        }       
        if(!$count_all){
            $html.="<span>1</span>  ";
        }else{
             for($i=1;$i<=min($num_btn,$pages);$i++,$start++){
            if($start>1&&$i==1){
                $html.="<a href='$_SERVER[SCRIPT_NAME]?{$data}&page=1'>1...</a>  ";    
            }elseif(($start-$i+1)<($pages-$num_btn+1)&&$i==$num_btn){
                $html.="<a href='$_SERVER[SCRIPT_NAME]?{$data}&page={$pages}'>...$pages</a>  ";
            }elseif($_GET['page']==$start){
                $html.="<span>$start</span>  ";
            }else{
                $html.="<a href='$_SERVER[SCRIPT_NAME]?{$data}&page=$start'>$start</a>  ";
            }
          }
        }
       if($_GET['page']!=$pages){
           $html.="<a href='$_SERVER[SCRIPT_NAME]?{$data}&page={$next}'>下一页 »</a>  ";
       }       
        $count=($_GET['page']-1)*$count_cur;
        if($pages<=1){
            $html="";
        }
        $datas=array(
            'html'=>"$html",
            'limit'=>"$count,$count_cur"
        );
        return $datas;
    }


?>